#include "skia_control/text.h"

#include <include/core/SkCanvas.h>

#include "skia_framework.h"

namespace SkiaControl {
Text::Builder::Builder()
{
    buildingText_ = std::shared_ptr<Text>(new Text());
}

void Text::Builder::SetRegion(const SkRect &region)
{
    buildingText_->region_ = region;
}

void Text::Builder::SetColor(const SkColor &color)
{
    buildingText_->color_ = color;
}

void Text::Builder::SetText(const std::string &text)
{
    buildingText_->text_ = text;
}

std::shared_ptr<Text> Text::Builder::Build()
{
    return buildingText_;
}

bool Text::IsContain(int32_t x, int32_t y)
{
    auto wh = SkiaFramework::MeasureString(text_);
    auto region = region_;
    if (region.width() == 0 || region.height() == 0) {
        region.fRight = region.fLeft + wh.x();
        region.fBottom = region.fTop + wh.y();
    }
    return region.contains(x, y);
}

void Text::SetText(const std::string &text)
{
    text_ = text;
}

void Text::OnDraw(SkCanvas &canvas)
{
    auto wh = SkiaFramework::MeasureString(text_);
    auto region = region_;
    if (region.width() == 0 || region.height() == 0) {
        region.fRight = region.fLeft + wh.x();
        region.fBottom = region.fTop + wh.y();
    }

    SkPoint offset{region.width() - wh.x(), region.height() - wh.y()};
    offset.scale(0.5);

    SkPaint paint;
    paint.setAntiAlias(true);
    paint.setColor(color_);
    paint.setStroke(paint.kFill_Style);
    SkiaFramework::DrawString(canvas, text_, region.x() + offset.x(), region.y() + offset.y() + wh.z());
}
} // namespace SkiaControl
